package com.yanqu.road.server.cmd.crossunion;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.strongholdwar.CrossStrongholaGetRankRewardCmd;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = CrossProtocol.C_CROSS_UNION_ACTIVITY_RANK_REWARD,desc = "")
public class CrossUnionActivityRewardCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossUnionActivityRankDataProto.CrossUnionActivityGetRewardReqMsg msg = CrossUnionActivityRankDataProto.CrossUnionActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        int activityId = msg.getActivityId();
        int conditionType = msg.getConditionType();
        int conditionId = msg.getConditionId();
        int rankType = msg.getRankType();
        long userId = packet.getPlayerId();
        UnionActivityConditionData unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, conditionType, userId);
        String unionUid = "";
        if (unionActivityConditionData != null) {
            unionUid = unionActivityConditionData.getUnionUid();
        }
        if (rankType == 1) {
            CrossActivityProto.CrossActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionId(conditionId);
            reqMsg.setUnionUid(unionUid);
            int position = getUnionPosition(unionActivityConditionData, userId);
            reqMsg.setPosition(position);
            YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_GET_REWARD, reqMsg);
            MessageHelper.sendPacket(serverId, userId, pkg);
        }else if (rankType == 2){

            unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, conditionType, userId);
            if (unionActivityConditionData == null) {
                unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, 88, userId);
            }
            unionUid = unionActivityConditionData.getUnionUid();
            CrossActivityProto.CrossActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionId(conditionId);
            reqMsg.setUnionUid(unionUid);
            int position = getUnionPosition(unionActivityConditionData, userId);
            reqMsg.setPosition(position);

            YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_STRONGHOLD_GET_RANK_REWARD, reqMsg);
//            CrossStrongholaGetRankRewardCmd cmd  = new CrossStrongholaGetRankRewardCmd();
//            cmd.parseData(pkg);
//            cmd.execute(pkg);
            MessageHelper.sendPacket(serverId, userId, pkg);
        }else if (rankType == 3){
            CrossActivityProto.CrossActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionId(conditionId);
            reqMsg.setUnionUid(unionUid);
            int position = getUnionPosition(unionActivityConditionData, userId);
            reqMsg.setPosition(position);
            YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_OREWAR_GET_RANK_REWARD, reqMsg);
            MessageHelper.sendPacket(serverId, userId, pkg);
        } else if (rankType == 9) {
            CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.Builder reqMsg = CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setConditionType(conditionType);
            reqMsg.setConditionId(conditionId);
            reqMsg.setUnionUid(unionUid);
            int position = getUnionPosition(unionActivityConditionData, userId);
            reqMsg.setPosition(position);
            YanQuMessage pkg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_GENERAL_ACTIVITY_GET_RANK_REWARD, reqMsg);
            MessageHelper.sendPacket(serverId, userId, pkg);
        }

    }

    @Override
    public void sendData() {

    }

    private int getUnionPosition(UnionActivityConditionData unionActivityConditionData, long userId){
        int position = 0;
        if(null == unionActivityConditionData){
            return position;
        }
        if(null != unionActivityConditionData.getMemberPositionMap()){
            if(unionActivityConditionData.getMemberPositionMap().containsKey(userId)){
                position = unionActivityConditionData.getMemberPositionMap().get(userId);
            }
        }
        if(0 == position){
            position = eUnionPosition.Member.getValue();
            if(unionActivityConditionData.getUnionBaseInfo().getMasterUserId() == userId){
                position = eUnionPosition.Master.getValue();
            }
        }
        return position;
    }
}
